package com.assistant.wechat.controller;

import com.assistant.admin.pojo.PageResult;
import com.assistant.admin.pojo.PageVariable;
import com.assistant.common.DateUtil;
import com.assistant.service.ItemsPostService;
import com.assistant.wechat.pojo.UserInfo;
import com.assistant.wechat.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService service;

    @Autowired
    private ItemsPostService itemsPostService;

    @PostMapping("/getUserInfoList")
    public PageResult<UserInfo> getUserInfoList(@RequestBody PageVariable pageVariable) {
        return service.getUserInfoList(pageVariable.getPage(), pageVariable.getRows(), pageVariable.getKey(), pageVariable.getStatus());
    }

    @GetMapping("/getUserInfoById/{id}")
    public ResponseEntity<Map<String, Object>> getUserInfo(@PathVariable("id") String id) {
        List<UserInfo> userInfo = service.getUserInfo(id);
        Map<String, Object> param = new HashMap<>();
        if (userInfo.size() == 0) {
            param.put("flag", false);
            param.put("msg", "未提交");
        }else {
            param.put("flag", "true");
            param.put("msg", "已提交");
            param.put("item", userInfo.get(0));
        }
        return  new ResponseEntity<Map<String, Object>>(param, HttpStatus.OK);
    }

    @DeleteMapping("/delUserInfoById/{id}")
    public Integer delUserInfoById(@PathVariable("id") Integer id) {
        return service.delUserInfo(id);
    }

    @PostMapping(value = "/addUserInfo", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Integer addUserInfo(UserInfo userInfo, @RequestPart("files") MultipartFile[] files) {
        if (files != null) {
            userInfo.setStu_card(itemsPostService.upLoadPic(files));//存入图片路径
        }
        userInfo.setPost_time(DateUtil.dataFormatUtil(new Date()));
        return service.insertUserInfo(userInfo);
    }

    /*
     * 通过审核
     * */
    @PutMapping("/updateUserInfoStatus/{id}/{openid}")
    public Integer updateUserInfoStatus(@PathVariable("id") Integer id, @PathVariable("openid") String openid) {
        return service.updateUserInfoStatus(id, openid);
    }

    /*
     * 驳回并返回驳回信息
     * */
    @PostMapping("/updateUserInfoRefuse")
    public Integer updateUserInfoRefuse(@RequestBody Map<String, String> map) {
        return service.updateUserInfoRefuse(map);
    }

    /*
    * 根据id查一条信息
    * */
    @GetMapping("/getUserInfoByInteger/{id}")
    public UserInfo getUserListById(@PathVariable("id") Integer id){
        return service.getUserInfoById(id).get(0);
    }


    @GetMapping("/UpdateValidated/{id}")
    public int UpdateValidated(@PathVariable("id") String id){
        return service.UpdateValidated(id);
    }

}
